<?php

namespace App\Http\Controllers\Home;

use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class SearchController extends CommonController
{
    public function index(Request $request, BaseServer $baseServer)
    {
        if (empty($request->key)) {
            $request->key = ' ';
        }

        $data['title'] = config('app.search_name') . '_' . config('set.web_name');

        $data['keywords'] = config('set.web_keywords');
        $baseServer->keywordDowork($request, $data['keywords']);

        $data['description'] = config('set.web_description');

        $data['chunks'] = \App\Chunk::where('status', 1)
            ->lists('name', 'id')
            ->toArray();
        if (!empty($request->key)) {
            $this->validate($request, [
                'chunk' => 'array',
            ]);
            $data['key'] = $request->key;
            $data['checks'] = $request->chunk;
            $search = str_replace(explode(',', config('set.web_keywords_long')), '', $request->key);
            //得到地区名称数据 用于去除关键词中的地区
            if (!\Cache::has('allAreas')) {
                $allAreas = \App\Area::lists('name')->toArray();
                \Cache::forever('allAreas', $allAreas);
            } else {
                $allAreas = \Cache::get('allAreas');
            }
            $search = str_replace($allAreas, '', $search);

            $data['frames'] = $baseServer->frame([
                ['url' => '', 'name' => $request->key . '搜索结果'],
            ]);

            $tmpSearchInfos = \App\Info::where('infos.status', 1)
                ->where('sorts.status', 1)
                ->where('chunks.status', 1)
//                ->whereIn('info_sort_lines.chunk', $request->chunk)
                ->where(function ($query) use ($request) {
                    if (!empty($request->chunk)) {
                        $query->whereIn('info_sort_lines.chunk', $request->chunk);
                    }
                })
                ->whereRaw('infos.name like "%' . $search . '%" or infos.tag like "%' . $search . '%" ')
                ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
                ->join('sorts', 'sorts.id', '=', 'info_sort_lines.sort')
                ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
                ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
                ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
                ->select('infos.*', 'infos.created_at as time', \DB::raw('group_concat(sorts.name) as sort'), 'chunks.name as chunk_name', 'chunks.path as chunk', 'images.path as image', 'images.water')
                ->groupBy('infos.id')
                ->get()
                ->toArray();
            foreach ($tmpSearchInfos as $k => $info) {
                if (!(bool)$info['id']) {
                    unset($tmpSearchInfos);
                } else {
                    $tmpSearchInfos['image'] = !empty($info['water']) ? $info['water'] : $info['image'];
                    $tmpSearchInfos['sort'] = implode(',', array_unique(explode(',', $info['sort'])));
                }

                $data['searchinfos'][$info['chunk_name']][] = $tmpSearchInfos[$k];
            }
        }

        return view('home.pc.search.index', $data);
    }
}
